package flc.ast.dialog;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.pdf.lib.model.IPdf2ImgListener;
import com.stark.pdf.lib.model.IPdfCreateListener;
import com.stark.pdf.lib.model.PdfUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import flc.ast.R;
import flc.ast.activity.PdfResultActivity;
import flc.ast.activity.PicResultActivity;
import flc.ast.databinding.DialogConvertTipBinding;
import stark.common.basic.base.BaseSmartDialog;
import stark.common.basic.utils.FileP2pUtil;

public class ConvertTipDialog extends BaseSmartDialog<DialogConvertTipBinding> implements View.OnClickListener {

    public int type;
    public String selPath = "";
    public List<String> listSel = new ArrayList<>();

    public ConvertTipDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_convert_tip;
    }

    @Override
    protected void initView(View view) {
        if (type == 0) {
            mDataBinding.tvContent.setText(getContext().getString(R.string.pdf_convert_pic_tip));
        } else {
            mDataBinding.tvContent.setText(getContext().getString(R.string.pic_convert_pdf_tip));
        }
        Random random = new Random();
        int num = random.nextInt(50) + 10;
        mDataBinding.sbProgress.setProgress(num);
        mDataBinding.tvProgress.setText(num + getContext().getString(R.string.unit_percent));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (type == 0) {
                    pdfConvImg();
                } else {
                    imgConvPdf();
                }
            }
        }, 500);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }

    //PDF转图片
    private void pdfConvImg() {
        PdfUtil.pdf2Img(selPath, new IPdf2ImgListener() {
            @Override
            public void onStart() {

            }

            @Override
            public void onEnd(int imageCount, List<String> outputFilePaths) {
                mDataBinding.sbProgress.setProgress(100);
                mDataBinding.tvProgress.setText(100 + getContext().getString(R.string.unit_percent));
                dismiss();
                if (outputFilePaths == null || outputFilePaths.size() == 0) {
                    ToastUtils.showShort(R.string.conv_def);
                } else {
                    String path = outputFilePaths.get(0);
                    PicResultActivity.savePath = path;
                    Intent intent = new Intent(getContext(), PicResultActivity.class);
                    getContext().startActivity(intent);
                }
            }
        });
    }

    //图片转PDF
    private void imgConvPdf() {
        PdfUtil.img2Pdf(listSel, new IPdfCreateListener() {
            @Override
            public void onStart() {

            }

            @Override
            public void onEnd(boolean success, String path) {
                mDataBinding.sbProgress.setProgress(100);
                mDataBinding.tvProgress.setText(100 + getContext().getString(R.string.unit_percent));
                dismiss();
                if (success) {
                    PdfResultActivity.savePath = path;
                    Intent intent = new Intent(getContext(), PdfResultActivity.class);
                    getContext().startActivity(intent);
                } else {
                    ToastUtils.showShort(getContext().getString(R.string.conv_def));
                }
            }
        });
    }


    private Listener listener;

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    public interface Listener {
        void clickConfirm(String path);
    }
}
